var express = require("express");
var router = express.Router();

// 引入封装的 mysql模块
const mysql = require("../util/mysql/mysql");

//主页
router.post("/home", function (req, res, next) {
  mysql.data("select * from data", function (err, rows, fields) {
    if (err) {
      res.send("错误");
      return;
    } else {
      res.send(JSON.stringify(rows));
    }
  });
});
//获取用户名称
router.post("/getUser", function (req, res, next) {
  const [u_id, u_type] = [req.body.u_id, req.body.u_type];
  switch (u_type) {
    case 1:
      mysql.data(
        "select s_name from student_mes WHERE s_id=?",
        [u_id],
        function (err, rows, fields) {
          if (err) {
            res.send("错误");
            return;
          } else {
            res.send(JSON.stringify({ name: rows[0].s_name }));
          }
        }
      );
      break;
    case 2:
      mysql.data(
        "select t_name from teacher_mes WHERE t_id=?",
        [u_id],
        function (err, rows, fields) {
          if (err) {
            res.send("错误");
            return;
          } else {
            res.send(JSON.stringify({ name: rows[0].t_name }));
          }
        }
      );
      break;
    case 3:
      mysql.data(
        "select e_name from expert_mes WHERE e_id=?",
        [u_id],
        function (err, rows, fields) {
          if (err) {
            res.send("错误");
            return;
          } else {
            res.send(JSON.stringify({ name: rows[0].e_name }));
          }
        }
      );
      break;
    default:
      break;
  }
});
//注册
router.post("/adduser", function (req, res, next) {
  mysql.data(
    "select * from user where u_user=" + req.body.u_user,
    [],
    function (err, rows, fields) {
      if (err) return;
      if (rows.length != 0) {
        res.send({ code: 0 });
      } else {
        mysql.data(
          "insert into user(u_user,u_pass) values(?,?)",
          [req.body.u_user, req.body.u_pass],
          function (err, rows, fields) {
            if (err) return;
            res.send('{"code":"2"}');
          }
        );
      }
    }
  );
});
//修改密码
router.post("/changePassword", function (req, res, next) {
  const [u_type, user_id, new_password, old_password] = [
    req.body.u_type,
    req.body.user_id,
    req.body.new_password,
    req.body.old_password,
  ];
  mysql.data(
    "select u_password from users_id WHERE u_type=? AND u_type_id=?",
    [u_type, user_id],
    function (err, rows1, fields) {
      if (err) return;
      if (rows1[0].u_password == old_password) {
        mysql.data(
          "UPDATE users_id SET u_password=? WHERE u_type=? AND u_type_id=?",
          [new_password, u_type, user_id],
          function (err, rows, fields) {
            if (err) {
              res.send("错误");
              return;
            } else {
              res.send(JSON.stringify({ code: 1 }));
            }
          }
        );
      } else {
        res.send(JSON.stringify({ code: 2 }));
      }
    }
  );
});
//获取学生列表
router.post("/getStudentList", function (req, res, next) {
  mysql.data("SELECT * FROM student_mes", [], function (err, rows, fields) {
    if (err) return;
    res.send(
      JSON.stringify({
        code: 1,
        studentList: rows,
      })
    );
  });
});
//获取老师列表
router.post("/getTeacherList", function (req, res, next) {
  mysql.data("SELECT * FROM teacher_mes", [], function (err, rows, fields) {
    if (err) return;
    res.send(
      JSON.stringify({
        code: 1,
        teacherList: rows,
      })
    );
  });
});
//获取专家列表
router.post("/getExpertList", function (req, res, next) {
  mysql.data("SELECT * FROM expert_mes", [], function (err, rows, fields) {
    if (err) return;
    res.send(
      JSON.stringify({
        code: 1,
        expertList: rows,
      })
    );
  });
});
//修改学生信息
router.post("/updateStudent", function (req, res, next) {
  const studentData = req.body.studentData;
  mysql.data(
    "UPDATE student_mes SET s_name=?,s_grade=?,s_shoolId=?,s_major=?,s_classname=?,s_phone=?,s_eMail=? WHERE s_id=?",
    [
      studentData.s_name,
      studentData.s_grade,
      studentData.s_shoolId,
      studentData.s_major,
      studentData.s_classname,
      studentData.s_phone,
      studentData.s_eMail,
      studentData.s_id,
    ],
    function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//修改老师信息
router.post("/updateTeacher", function (req, res, next) {
  const teacherData = req.body.teacherData;
  mysql.data(
    "UPDATE teacher_mes SET t_name=?,t_department=?,t_schoolId=?,t_post=?,t_site=?,t_phone=?,t_eMail=? WHERE t_id=?",
    [
      teacherData.t_name,
      teacherData.t_department,
      teacherData.t_schoolId,
      teacherData.t_post,
      teacherData.t_site,
      teacherData.t_phone,
      teacherData.t_eMail,
      teacherData.t_id,
    ],
    function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//修改专家信息
router.post("/updateExpert", function (req, res, next) {
  const expertData = req.body.expertData;
  mysql.data(
    "UPDATE expert_mes SET e_name=?,e_department=?,e_schoolId=?,e_post=?,e_site=?,e_phone=?,e_eMail=? WHERE e_id=?",
    [
      expertData.e_name,
      expertData.e_department,
      expertData.e_schoolId,
      expertData.e_post,
      expertData.e_site,
      expertData.e_phone,
      expertData.e_eMail,
      expertData.e_id,
    ],
    function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//添加奖项
router.post("/addAward", function (req, res, next) {
  const addAwardData = req.body.addAwardData;
  const userID = req.body.user_id;
  mysql.data(
    "INSERT INTO awards (a_title,a_time,a_type,a_user_id) VALUES (?,?,?,?)",
    [addAwardData.a_title, addAwardData.a_time, addAwardData.a_type, userID],
    function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//移除奖项
router.post("/delAward", function (req, res, next) {
  const a_id = req.body.a_id;
  mysql.data(
    "DELETE FROM awards WHERE a_id=?",
    [a_id],
    function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//获取可分配项目
router.post("/allotproject", function (req, res, next) {
  mysql.data(
    "select * from project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON a.id = b.p_id AND a.p_teacher_id=c.t_id WHERE p_expert_id=0",
    [],
    function (err, rows, fields) {
      if (err) return;
      res.send(
        JSON.stringify({
          code: 1,
          allotproject: rows,
        })
      );
    }
  );
});
//分配项目
router.post("/allotPro", function (req, res, next) {
  const [e_id, p_id] = [req.body.e_id, req.body.p_id];
  mysql.data(
    "UPDATE project SET p_expert_id=? WHERE id=?",
    [e_id, p_id],
    function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//获取管理员信息
router.post("/getAdmMes", function (req, res, next) {
  mysql.data(
    "SELECT * FROM administrator_mes WHERE a_id",
    [req.body.a_id],
    function (err, rows1, fields) {
      if (err) return;
      mysql.data("SELECT * FROM system_mes", [], function (err, row2s, fields) {
        if (err) return;
        res.send(
          JSON.stringify({
            code: 1,
            mes: rows1[0],
            systemMes:row2s
          })
        );
      });
    }
  );
});
//发布系统通知
router.post("/writeSystemInf", function (req, res, next) {
  const system_inf = req.body.system_inf;
  const time = req.body.time;
  mysql.data(
    "INSERT INTO system_mes (system_inf,time) VALUES (?,?)",
    [system_inf,time],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return}
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
module.exports = router;
